package com.neuedu.system.service.impl;
import lombok.RequiredArgsConstructor;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.neuedu.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.neuedu.system.domain.Comments;
import com.neuedu.system.mapper.PostsMapper;
import com.neuedu.system.domain.Posts;
import com.neuedu.system.service.IPostsService;

/**
 * 帖子Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-18
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PostsServiceImpl extends ServiceImpl<PostsMapper, Posts> implements IPostsService
{
    private final PostsMapper postsMapper;

    /**
     * 查询帖子
     *
     * @param postId 帖子主键
     * @return 帖子
     */
    @Override
    public Posts selectPostsByPostId(Long postId)
    {
        return postsMapper.selectPostsByPostId(postId);
    }

    /**
     * 查询帖子列表
     *
     * @param posts 帖子
     * @return 帖子
     */
    @Override
    public List<Posts> selectPostsList(Posts posts)
    {
        return postsMapper.selectPostsList(posts);
    }

    /**
     * 新增帖子
     *
     * @param posts 帖子
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPosts(Posts posts)
    {
        int rows = postsMapper.insertPosts(posts);
        insertComments(posts);
        return rows;
    }

    /**
     * 修改帖子
     *
     * @param posts 帖子
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePosts(Posts posts)
    {
        postsMapper.deleteCommentsByPostId(posts.getPostId());
        insertComments(posts);
        return postsMapper.updatePosts(posts);
    }

    /**
     * 批量删除帖子
     *
     * @param postIds 需要删除的帖子主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePostsByPostIds(Long[] postIds)
    {
        postsMapper.deleteCommentsByPostIds(postIds);
        return postsMapper.deletePostsByPostIds(postIds);
    }

    /**
     * 删除帖子信息
     *
     * @param postId 帖子主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePostsByPostId(Long postId)
    {
        postsMapper.deleteCommentsByPostId(postId);
        return postsMapper.deletePostsByPostId(postId);
    }

    /**
     * 新增评论信息
     *
     * @param posts 帖子对象
     */
    public void insertComments(Posts posts)
    {
        List<Comments> commentsList = posts.getCommentsList();
        Long postId = posts.getPostId();
        if (StringUtils.isNotNull(commentsList))
        {
            List<Comments> list = new ArrayList<Comments>();
            for (Comments comments : commentsList)
            {
                comments.setPostId(postId);
                list.add(comments);
            }
            if (list.size() > 0)
            {
                postsMapper.batchComments(list);
            }
        }
    }
}
